package com.example.demothree.controller;

import com.example.demothree.bean.*;
import com.example.demothree.service.PurService;
import com.example.demothree.service.SkService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("pur")
public class PurController {
    @Resource
    private PurService purService;

    @Resource
    private SkService skService;

    private PurchaseOrder purchaseOrder;
    private ReturnOrder returnOrder;
    private PurchaseItem purchaseItem;
    private Goods goods;
    private Stock stock;

    private static Long purId = 0L;
    //采购订单
    @RequestMapping( "purcgdd")
    public String purcgdd(HttpServletRequest req, HttpServletResponse res, Model model,//req接收前端数据，res向前端传输数据
                          @RequestParam(value = "page", defaultValue = "0") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size) throws IOException {
        if (this.purId != 0L){//判断是否删除成功
            Long deleteCgdd1 = this.purId;
            model.addAttribute("deleteCgdd1",deleteCgdd1);
        }
        this.purId = 0L;
        //分页
        Page<PurchaseOrder> page1 = purService.purcgdd(page,size);
        model.addAttribute("page1",page1);
        return "purcgdd";
    }
    //退货订单
    @GetMapping("purcgddDelete/{id}")
    public String purcgddDelete(@PathVariable Long id) {
        try {
            //退货订单添加
            purchaseOrder = purService.findById(id);
            Long goodsId = purchaseOrder.getGoodsId();
            String pay = purchaseOrder.getPay();
            Long suppliersId = purchaseOrder.getSuppliersId();
            Double total = purchaseOrder.getTotal();
            Long count = purchaseOrder.getCount();
            returnOrder = new ReturnOrder();
            returnOrder.setGoodsId(goodsId);
            returnOrder.setPay(pay);
            returnOrder.setSuppliersId(suppliersId);
            returnOrder.setTotal(total);
            returnOrder.setCount(count);
            //添加当前时间
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            Date date1 = dateFormat.parse(dateFormat.format(date));
            returnOrder.setOutDate(date1);
            if (purchaseOrder.getFlag().equals("已入库")) {
                stock = skService.findspidBy(purchaseOrder.getGoodsId());
                if (stock.getCounts()>purchaseOrder.getCount()) {
                    stock.setCounts(stock.getCounts() - purchaseOrder.getCount());
                    skService.addkc(stock);
                    purService.addcgth(returnOrder);
                    //采购订单按id退货
                    purService.deletucgdd(Long.valueOf(id));
                    //退货弹窗
                    this.purId = id;
                }else {
                    this.purId = -1L;
                }
            }else {
                purService.addcgth(returnOrder);
                //采购订单按id退货
                purService.deletucgdd(Long.valueOf(id));
                //退货弹窗
                this.purId = id;
            }
        } catch (Exception e) {
            this.purId = -2L;
            e.printStackTrace();
        }
        return "forward:/pur/purcgdd";
    }
    @RequestMapping("adddd")
    public String adddd(Model model,HttpServletRequest request) {
        if (this.purId.equals(1L)) {
            model.addAttribute("purchaseOrder",purchaseOrder);
        }else {
            int a = 1;
            System.out.println(a);
            model.addAttribute("a", a);
    }
        this.purId = 0L;
        return "adddd";
    }
    @GetMapping("addddcz/{id}")
    public String addddcz(@PathVariable Long id) {
        if (id != null) {
            purchaseOrder = purService.findById(Long.valueOf(id));
            this.purId = 1L;
        }
        return "forward:/pur/adddd";
    }
    //添加订单
    @RequestMapping("addddzc")
    public String addddzc(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        String goodsId = request.getParameter("goodsId");
        String supplierId = request.getParameter("supplierId");
        String pay = request.getParameter("pay");
        String date = request.getParameter("date");
        String count = request.getParameter("count");
        String total = request.getParameter("total");
        String flag = request.getParameter("flag");
        System.out.println(id);
        int ddadd;
        try {
            if (id != null) {
                //修改
                purchaseOrder = purService.findById(Long.valueOf(id));
            }else {
                //添加
                purchaseOrder = new PurchaseOrder();
            }
            purchaseOrder.setGoodsId(Long.valueOf(goodsId));
            purchaseOrder.setSuppliersId(Long.valueOf(supplierId));
            purchaseOrder.setPay(pay);
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date myDate1 = dateFormat.parse(date);
            purchaseOrder.setInDate(myDate1);
            purchaseOrder.setCount(Long.valueOf(count));
            purchaseOrder.setTotal(Double.valueOf(total));
            purchaseOrder.setFlag(flag);
            purService.adddd(purchaseOrder);
            ddadd = 100;
        }catch (Exception e) {
            ddadd = 200;
            e.printStackTrace();
        }
        response.getWriter().print(ddadd);
        return null;
    }

    //采购退货
    @RequestMapping("purcgth")
    public String purcgth(Model model,
                          @RequestParam(value = "page", defaultValue = "0") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size) throws IOException {
        if (this.purId != 0L){//判断是否删除成功
            Long deleteCgth1 = this.purId;
            model.addAttribute("deleteCgth1",deleteCgth1);
        }
        this.purId = 0L;
        //分页
        Page<ReturnOrder> page2 = purService.purcgth(page,size);
        model.addAttribute("page2",page2);
        return "purcgth";
    }
    //删除退货订单
    @GetMapping("purcgthDelete/{id}")
    public String purcgthDelete(@PathVariable Long id) {
        purchaseOrder = purService.findById(id);
            //删除退货订单弹窗
            this.purId = id;
            //按id删除退货订单
            purService.deletecgth(Long.valueOf(id));
        return "forward:/pur/purcgth";
    }
    //收货入库
    @RequestMapping("purshrk")
    public String purshrk(HttpServletRequest req, Model model,
                          @RequestParam(value = "page", defaultValue = "0") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size) {
        if (this.purId != 0L){//判断是否入库成功
            Long deleteShrk1 = this.purId;
            model.addAttribute("deleteShrk1",deleteShrk1);
        }
        this.purId = 0L;
        //分页
        Page<PurchaseOrder> page3 = purService.purcgdd(page,size);
        model.addAttribute("page3",page3);
        return "purshrk";
    }
    //入库
    @GetMapping("purshrkrk/{id}")
    public String purshrkDelete(@PathVariable Long id) {
        try {
            //库存入库
            purchaseOrder = purService.findById(id);
            if (purchaseOrder.getFlag().equals("未入库")) {
                Long a = purchaseOrder.getCount();
                Long goodsId = purchaseOrder.getGoodsId();
//                if (stock.getGoodsId()==null) {
                    stock = skService.findspidBy(goodsId);
                    if (stock==null){
                        stock = new Stock();
                        stock.setArea("临时仓库");
                        stock.setCounts(purchaseOrder.getCount());
                        stock.setGoodsId(purchaseOrder.getGoodsId());
                    }else if (goodsId.equals(stock.getGoodsId())) {
                        stock.setCounts(stock.getCounts() + a);
                    }  else {

                    }
//                }
                skService.addkc(stock);

                purchaseOrder.setFlag("已入库");
                purService.adddd(purchaseOrder);

                //入库弹窗
                this.purId = id;
            }else {
                this.purId = -2L;
            }
        } catch (Exception e) {
            this.purId = -1L;
            System.out.println(purId);
            e.printStackTrace();
        }
        return "forward:/pur/purshrk";
    }
    @RequestMapping("addsp")
    public String addsp() {
        return "addsp";
    }
    //添加商品
    @RequestMapping( "addpur")
    @ResponseBody
    public String addpur(HttpServletRequest request,HttpServletResponse response ) throws Exception {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String unit = request.getParameter("unit");
        String space = request.getParameter("space");
        String supplierId = request.getParameter("price");
        String price = request.getParameter("price");
        String approveId = request.getParameter("approveId");
        String batchId = request.getParameter("batchId");
        String count = request.getParameter("count");
        goods = new Goods();
        Goods goods1 =  purService.findByName(name);
        int puradd = 0;
        if (goods1 == null) {
            try {
                goods.setName(name);
                goods.setUnit(unit);
                goods.setSpace(space);
                goods.setSupplierId(Long.parseLong(supplierId));
                goods.setPrice(Double.valueOf(price));
                goods.setApproveId(approveId);
                goods.setBatchId(batchId);
                purService.saveGoods(goods);
                //
                purchaseItem = new PurchaseItem();
                purchaseItem.setCount(Long.valueOf(count));
                purchaseItem.setName(String.valueOf(name));
                purchaseItem.setSupplierId(Long.valueOf(id));
                purchaseItem.setPrice(Double.valueOf(price));
                purchaseItem.setCount(Long.valueOf(count));
                purchaseItem.setPurchaseOrderId(Long.valueOf(id));
                purService.savePurchaseItem(purchaseItem);
                puradd = 100;

            } catch (Exception e) {
                puradd = 200;
                e.printStackTrace();
            }
        }else {

        }
        response.getWriter().print(puradd);
        return null;
    }
}
